package com.cloudfun.campusshare.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.cloudfun.campusshare.common.constant.AttachType;
import com.cloudfun.campusshare.common.constant.WorkerOrderStatus;
import com.cloudfun.campusshare.common.constant.WorkerOrderTaskType;
import com.cloudfun.campusshare.common.model.dto.RepairCreateDTO;
import com.cloudfun.campusshare.common.security.AuthenticationHolder;
import com.cloudfun.campusshare.entity.DeviceEntity;
import com.cloudfun.campusshare.entity.RepairEntity;
import com.cloudfun.campusshare.entity.UserEntity;
import com.cloudfun.campusshare.entity.WorkerOrderEntity;
import com.cloudfun.campusshare.repo.DeviceEntityRepo;
import com.cloudfun.campusshare.repo.RepairEntityRepo;
import com.cloudfun.campusshare.repo.UserEntityRepo;
import com.cloudfun.campusshare.repo.WorkerOrderEntityRepo;
import com.cloudfun.campusshare.service.AttachmentService;
import com.cloudfun.campusshare.service.RepairService;
import com.cloudfun.campusshare.util.OrderNoGenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.stream.Collectors;

/**
 * Created by Huqin on 2020/4/22
 */
@Slf4j
@Service
public class RepairServiceImpl implements RepairService {

    @Autowired
    private RepairEntityRepo repairEntityRepo;

    @Autowired
    private AttachmentService attachmentService;

    @Resource
    private WorkerOrderEntityRepo workerOrderEntityRepo;

    @Resource
    private DeviceEntityRepo deviceEntityRepo;

    @Resource
    private UserEntityRepo userEntityRepo;

    @Autowired
    private OrderNoGenUtil orderNoGenUtil;

    @Override
    @Transactional
    public void create(RepairCreateDTO createDTO) {
        RepairEntity repairEntity = new RepairEntity();
        BeanUtil.copyProperties(createDTO, repairEntity, "faultType");
        DeviceEntity device = deviceEntityRepo.findByImei(createDTO.getImei());
        repairEntity.setDeviceId(device.getId());
        repairEntity.setUserId(AuthenticationHolder.getUserId());
        repairEntity.setFaultType(createDTO.getFaultType().stream().map(x -> x.name()).collect(Collectors.joining(",")));
        RepairEntity save = repairEntityRepo.save(repairEntity);
        // 关联附件
        attachmentService.relateAttachment(repairEntity.getId(), createDTO.getAttachIds(), AttachType.DEVICE_REPAIR);
        //创建工单
        createWorkerOrder(save.getId(), createDTO.getImei());
    }

    private void createWorkerOrder(String repairId, String imei) {
        WorkerOrderEntity workerOrderEntity = new WorkerOrderEntity();
        workerOrderEntity.setTaskType(WorkerOrderTaskType.DEVICE_REPAIR);
        workerOrderEntity.setBusinessId(repairId);
        //提交时间
        workerOrderEntity.setSubmitTime(LocalDateTime.now());
        workerOrderEntity.setOpenedTime("0天0时0分");
        //过期时间 天数+2
        workerOrderEntity.setExpireTime(LocalDateTime.now().plusDays(2));
        DeviceEntity device = deviceEntityRepo.findByImei(imei);
        String merchantId = device.getUserId();
        workerOrderEntity.setMerchantId(merchantId);
        String username = userEntityRepo.findById(merchantId).map(UserEntity::getUsername).orElse("");
        //当前处理人，创建时分给运营商管理员
        workerOrderEntity.setTransactorId(merchantId);
        workerOrderEntity.setTransactorName(username);
        //打开工单状态
        workerOrderEntity.setWorkerOrderStatus(WorkerOrderStatus.WAIT_POINT);
        workerOrderEntity.setBelongMerchantName(device.getBelongMerchantName());
        workerOrderEntity.setWorkerOrderNo(orderNoGenUtil.next4WorkerOrder());
        workerOrderEntity.setWashPointId(device.getWashPointId());
        workerOrderEntity.setWashPointName(device.getWashPointName());
        workerOrderEntityRepo.save(workerOrderEntity);
    }

}
